package com.peng.leetcode.search;

/**
 * SearchInsert
 * 35. 搜索插入位置
 * https://leetcode.cn/problems/search-insert-position/
 * <p>
 * Created on 2022/12/10
 *
 * @author lupeng
 */
public class SearchInsert {

    /**
     * 左闭右开，返回第一个等于target的元素下标
     */
    public int searchInsert(int[] nums, int target) {
        int l = 0, r = nums.length;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] < target) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }
        return l;
    }
}
